{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Problem definition\n", "Tina Belcher, brand-new manager of the new Bob´s burger parlour downtown San Francisco, wants to analyse the impact on the market and the loyalty of the customers of Bob’s Burger and Pesto´s, an Italian restaurant nearby. It is assumed that one customer dines once a week at Bob’s or at Pesto´s, but not at both.\n", "Tina has collected information from 100 consumers over a 10-week period. When checking the data, she discovered that of all the customers who consumed at Bob’s in a given week, 90 % returned to the restaurant following week, while 10 % switched to Pesto’s. Of all the consumers who consumed at Pesto’s in a given week, 80 % returned to the same place the following week, but 20 % switched to Bob´s.\n", "Assuming that the transition probabilities are the same for every customer and that they not change over time, we can get insights modelling customer fidelity with Markov chains.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model\n", "We model the decision of customers as a Markov chain, assuming two possible states every week (which is the discrete event period of our model). The states are: \n", "\n", "- $s_1$: represents the customers decision to go to Bob´s Burger.\n", "- $s_2$: represents the customers decision to go to Pestos. \n", "\n", "Now, with the data collected by Tina, we can estimate the transition probability Matrix. \n", "\n", "- $p_{11}$: It is the probability that a customer goes back to Bob´s Burger after they have been to Bob´s Burger. Tina discovered that this probability is 0.9 or 90%.\n", "- $p_{12}$: It is the probability that a customer goes to Pesto´s after going to Bob´s Burger. Tina estimates that this probability is 10%. Clearly, it has to be ($1-p_{11}=0.1$) since we only consider two possible states and at any given time, the system has to be in one particular state, so the transition probabilities need to add up to 1.\n", "- $p_{11}$: It is the probability that a customer goes back to Bob´s Burger after they have been to Bob´s Burger. Tina discovered that this probability is 0.9 or 90%.\n", "- $p_{21}$: It is the probability that a customer goes to Bob´s Burger after going to Pesto´s. Tina estimates this probability to be 0.2.\n", "- $p_{22}$: It is the probability that a customer goes to Pesto´s for two consecutive week, 0.8 according to Tina´s data. \n", "\n", "With this, the transition probability matrix is:\n", "\n", "\n", "$P^{(1)} =\n", "\\begin{bmatrix}\n", "0.9 & 0.1\\\\\n", "0.2 & 0.8\n", "\\end{bmatrix}$\n", "\n", "\n", "We can use Graphviz to represent the Markov chain:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "from graphviz import Digraph\n", "# Create Digraph object\n", "din = Digraph(format='png')\n", "\n", "# Add Bob´s Burger node\n", "din.node('s1', shape='circle', style=\"filled\")\n", "\n", "# Add Pesto´s node\n", "din.node('s2', shape='circle', style=\"filled\")\n", "\n", "# Add edges\n", "din.edge('s1', 's1', '0.9')\n", "din.edge('s1', 's2', '0.1')\n", "\n", "din.edge('s2', 's1', '0.2')\n", "din.edge('s2', 's2', '0.8')\n", "\n", "din" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Now, let us answer some questions:\n", "\n", "**a)** If at week 0, a customer dines at Bob’s, what is the probability of the same customer dining at Bob´s in week 1?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tina estimated that this is 90%, it is also possible to calculate it using the initial status and the probability Matrix, just in case we want to get more insights about this scenario. Since we are analysing a customer that goes to Bob´s at time 0, the probability vector is:\n", "\n", "$V_0 = [p_1, p_2] = [1, 0]$\n", "\n", "The, at t=1, the probability vector becomes: \n", "\n", "$V_1 = V_0·P^{(1)} = [1, 0]·\\begin{bmatrix}\n", "0.9 & 0.1\\\\\n", "0.2 & 0.8\n", "\\end{bmatrix} = [0.9, 0.1]$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b)** What is the probability of a customer going back to Bob´s two weeks running?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, in a Markov system, the possibility of a sequence of states, not taking into consideration any initial state and considering that the probability only depends on the previous state, is the product of the corresponding transition probabilities. In this example, we are analysing a sequence $s_1, s_1, s_1$, where the customer goes back to Bob's two consecutive weeks (or in other words, the customer dines at Bobs for three consecutive weeks). If we do not consider any initial state, the probability is:\n", "\n", "$p_{11}·p_{11} = 0.9·0.9 = 0.81$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**c)** What is the probability of a customer changing to Pesto´s after dining at Bob´s and then going back to Bob´s\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, if we do not consider any initial state, we are analysing a sequence $s_1, s_2, s_1$, and the probability of this sequence is: \n", "\n", "$p_{12}·p_{21} = 0.1·0.2 = 0.02$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**d)** Consider the customer of section a) what is the probability that this customer comes back to Bob´s in two weeks time?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, coming back to the first example, we have an initial state, and now we want to consider the two-step transition probability matrix:\n", "\n", "$V_2 = V_0·P^{(2)} = [1, 0]·\\begin{bmatrix}\n", "0.83 & 0.17\\\\\n", "0.34 & 0.66\n", "\\end{bmatrix} = [0.83, 0.17]$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**e)** What is the expected market share of customers between the two restaurants?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can calculate the stationary probabilities representing the probability that a customer is in a restaurant when k tends to infinity. In this case, we need to solve the following linear system of equations:\n", "\n", "$\\pi_1 = 0.9·\\pi_1 + 0.2·\\pi_2$\n", "\n", "$\\pi_2 = 0.1·\\pi_1 + 0.8·\\pi_2$\n", "\n", "$\\pi_1 + \\pi_2 = 1$\n", "\n", "This yields:\n", "\n", "$0.1·\\pi_1 = 0.2·\\pi_2$\n", "\n", "$\\pi_2 = 1/3$\n", "\n", "$\\pi_1 = 2/3$\n", "\n", "That is, in the long run, it is expected that 66.67% of customers are at state 1 (Bob´s Burger) and 33.33% of customers at state 2 (Pesto´s)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 2 }